/***************************************************************************
 *   Copyright (C) 2008 by Thomas Fannes                                   *
 *   thomasfannes@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "HexVector.h"

namespace structure
{
	HexVector::HexVector() :
		x(0), y(0), z(0)
	{
	}

	HexVector::HexVector(int iX, int iY, int iZ) :
		x(iX), y(iY), z(iZ)
	{
		toCanonical();
	}

	HexVector::HexVector(const HexVector & other) :
		x(other.x), y(other.y), z(other.z)
	{
		toCanonical();
	}

	HexVector::~HexVector()
	{
	}

	HexVector & HexVector::operator=(const HexVector & rhs)
	{
		x = rhs.x;
		y = rhs.y;
		z = rhs.z;

		toCanonical();

		return (*this);
	}

	HexVector HexVector::operator+(const HexVector & rhs) const
	{
		return HexVector(rhs.x + x, rhs.y + y, rhs.z + z);
	}

	HexVector HexVector::operator-(const HexVector & rhs) const
	{
		return HexVector(x - rhs.x, y - rhs.y, z - rhs.z);
	}

	HexVector HexVector::operator-() const
	{
		return HexVector(-x, -y, -z);
	}

	HexVector & HexVector::operator+=(const HexVector & rhs)
	{
		x += rhs.x;
		y += rhs.y;
		z += rhs.z;

		toCanonical();

		return (*this);
	}

	HexVector & HexVector::operator-=(const HexVector & rhs)
	{
		x -= rhs.x;
		y -= rhs.y;
		z -= rhs.z;

		return (*this);
	}

	bool HexVector::operator==(const HexVector & rhs) const
	{
		return x == rhs.x && y == rhs.y && z == rhs.z;
	}

	bool HexVector::operator!=(const HexVector & rhs) const
	{
		return x != rhs.x || y != rhs.y || z != rhs.z;
	}

	bool HexVector::operator<(const HexVector & rhs) const
	{
		return (getX() < rhs.getX() || (getX() == rhs.getX() && getY() < rhs.getY()) || (getX() == rhs.getX() && getY() == rhs.getY() && getZ() < rhs.getZ()));
	}

	bool HexVector::operator>(const HexVector & rhs) const
	{
		return !(operator<(rhs) || operator==(rhs));
	}

	void HexVector::setX(int iX)
	{
		x = iX;
		toCanonical();
	}

	void HexVector::setY(int iY)
	{
		y = iY;
		toCanonical();
	}

	void HexVector::setZ(int iZ)
	{
		z = iZ;
		toCanonical();
	}

	void HexVector::setCoordinates(int iX, int iY, int iZ)
	{
		x = iX;
		y = iY;
		z = iZ;

		toCanonical();
	}

	void HexVector::toCanonical()
	{
		int diff = (x + y + z) / 3;

		x -= diff;
		y -= diff;
		z -= diff;

	}

	int HexVector::getSum() const
	{
		return x + y + z;
	}

	float HexVector::getDistance(const HexVector & other) const
	{
		return (std::abs(getX() - other.getX()) + std::abs(getY() - other.getY()) + std::abs(getZ() - other.getZ())) / 12.0f;
	}

	float HexVector::getCenterDistance() const
	{
		return (std::abs(getX()) + std::abs(getY()) + std::abs(getZ())) / 12.0f;
	}

	float HexVector::getCenterDistance(const HexVector & vct)
	{
		return vct.getCenterDistance();
	}

	float HexVector::getDistance(const HexVector & vct1, const HexVector & vct2)
	{
		return vct1.getDistance(vct2);
	}
}
